#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include "getopt.h"
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>


#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a)/sizeof(a[0]))
#endif
#define LEN_HINT (500)
#define LEN_OPTION_SHORT 512  

#if 0
// #define DEBUG_LOG printf
#else
#define DEBUG_LOG(...)
#endif
static int test_mode=0;
/*
ubuntu修改时区
输入“timedatectl”命令检查当前是日期和时区设置。

获取时区列表
timedatectl list-timezones
修改时区
sudo timedatectl set-timezone Asia/Shanghai

*/

#if 0
 
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
 
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
 
time_t mktime(struct tm *tm);
 
 
struct tm {
   int tm_sec;    /* Seconds (0-60) */
   int tm_min;    /* Minutes (0-59) */
   int tm_hour;   /* Hours (0-23) */
   int tm_mday;   /* Day of the month (1-31) */
   int tm_mon;    /* Month (0-11) */
   int tm_year;   /* Year - 1900 */
   int tm_wday;   /* Day of the week (0-6, Sunday = 0) */
   int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */
   int tm_isdst;  /* Daylight saving time */
};
		   
struct timeval {
   time_t      tv_sec;     /* seconds */
   suseconds_t tv_usec;    /* microseconds */
};
 
struct timezone {
   int tz_minuteswest;     /* minutes west of Greenwich */
   int tz_dsttime;         /* type of DST correction */
};
		   
void tzset (void);
 
extern char *tzname[2];
extern long timezone;
extern int daylight;
	   
#endif
static int teatall(void);

void print_tm(struct tm *tm)
{
	if(tm){
		printf("tm_year = %04d\n", tm->tm_year);
		printf("tm_mon = %02d\n", tm->tm_mon);
		printf("tm_mday = %02d\n", tm->tm_mday);
		printf("tm_hour = %02d\n", tm->tm_hour);
		printf("tm_min = %02d\n", tm->tm_min);
		printf("tm_sec = %02d\n", tm->tm_sec);
		printf("tm_wday = %d\n", tm->tm_wday);
		printf("tm_yday = %03d\n", tm->tm_yday);
		printf("tm_isdst = %d\n", tm->tm_isdst);
		
		printf("%04d-%02d-%02d %02d:%02d:%02d\n",\
								tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,\
								tm->tm_hour, tm->tm_min, tm->tm_sec);
	}else{
		printf("NULL tm!\n");
	}
}
 
void print_tv(struct timeval *tv)
{
	if(tv){
		printf("tv->tv_sec = %ld\n", tv->tv_sec);
		printf("tv->tv_usec = %ld\n", tv->tv_usec);
	}
}
 
void print_tz(struct timezone *tz)
{
	printf("tz->tz_minuteswest = %d\n", tz->tz_minuteswest);
	printf("tz->tz_dsttime = %d\n", tz->tz_dsttime);
}


void print_tz_info(void)
{
	printf("tzname[0] = %s\n", tzname[0]);
	printf("tzname[1] = %s\n", tzname[1]);
	printf("timezone = %ld\n", timezone);
}

/*
    time_zone:-12 - +12
    例如，东八区，time_zone=-8
    中央时区，time_zone=0
    GMT+08:00 -> time_zone=-8

*/
int set_time_zone(int time_zone)
{
    int ret =0;
	int tzfilefd =0;
	const char*tzfilepath="/etc/TZ";
    int tmp = time_zone>=0? time_zone:time_zone*-1;
    char buf[20]; 
    if(time_zone <-12 ||time_zone>12 ){
        return -1;
    }
    memset(buf,0,sizeof(buf));
    sprintf(buf,"UTC%c%d",time_zone>=0 ? '+':'-',  tmp);
    setenv("TZ", buf, 1);//设置时区;
    tzset();
	tzfilefd =open(tzfilepath,O_WRONLY|O_CREAT,0644);
	if(tzfilefd<0){
		perror("open /etc/TZ failed!");
		return 0;
	}
	// printf("tz file fd:%d\n",tzfilefd);
	write(tzfilefd,buf,strlen(buf));
	close(tzfilefd);
    return ret;
}
static int test1(void)
{
    int ret =0;
    struct timeval tnt;
    struct timezone tzon;
    ret= gettimeofday(&tnt, &tzon);
    if(ret<0 ){
        perror("gettimeofday err");
    }
    settimeofday(&tnt, &tzon);
    if(ret<0 ){
        perror("settimeofday err");
    }
    
    struct tm *ptm;
	struct tm tm;
	struct timeval tv;
	struct timezone tz;
	time_t time;
    const char *tz_env=NULL;
	
	ret = gettimeofday(&tv, &tz);
	
	if(ret) {
		printf("------------gettimeofday Err\n");
		return ret;
	}
	
	printf("+++++++++++++++++++++gettimeofday+++++++++++++++++++++\n");
	print_tv(&tv);
	print_tz(&tz);
	
	time = tv.tv_sec;
	
	printf("+++++++++++++++++++++localtime+++++++++++++++++++++\n");
	ptm = localtime(&time);
	print_tm(ptm);
 
	printf("+++++++++++++++++++++localtime_r+++++++++++++++++++++\n");
	localtime_r(&time, &tm);
	print_tm(&tm);
	
	printf("+++++++++++++++++++++localtime_r -> mktime+++++++++++++++++++++\n");
	printf("mktime = %ld\n", mktime(&tm));
	
	printf("+++++++++++++++++++++gmtime+++++++++++++++++++++\n");
	ptm = gmtime(&time);
	print_tm(ptm);
	
	printf("+++++++++++++++++++++gmtime_r+++++++++++++++++++++\n");
	gmtime_r(&time, &tm);
	print_tm(&tm);
	
	
	printf("+++++++++++++++++++++print_tz_info+++++++++++++++++++++\n");
	//tzset();
	print_tz_info();

	printf("+++++++++++++++++++++Read TZ ENV+++++++++++++++++++++\n");
    tz_env =getenv("TZ");
    if(tz_env){
        printf("TZ ENV:%s\n",tz_env);
    }
	printf("+++++++++++++++++++++Set TZ ENV+++++++++++++++++++++\n");
    setenv("TZ", "CST-8", 1);
    tzset();
	print_tz_info();


    return ret ;
}

static int test2(void)
{
	time_t now = 0; char * TZ = NULL;  struct tm *tm_ = NULL; struct timeval tval = {0,0}; struct timezone tzone = {0,0};
	TZ = getenv("TZ");
	printf("====================================> TZ = %s\n", TZ == NULL?"env TZ not seted" : TZ );
	now = time(NULL); //1970到现在的UTC时间的秒数；
	printf("time_t = %ld, ctime: = %s", now, ctime(&now) ); //char* ctime(const time_t *timer)返回一个表示当地时间的字符串(包含一个换行符)，当地时间是基于参数 timer。
	gettimeofday(&tval, &tzone);
	printf("gettimeofday tval.tv_sec = %ld, tval.tv_usec = %ld; tzone.tz_minuteswest = %d, tzone.tz_dsttime = %d.\n", \
			tval.tv_sec, tval.tv_usec, tzone.tz_minuteswest, tzone.tz_dsttime);
 
	tm_ = gmtime(&now);                  //转换成UTC世界时间
	printf("gmtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec);   
 
	tm_ = localtime(&now);               //转换成本地时间
	printf("localtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec); 
	printf("\n");
 
 
 
	setenv("TZ", "CST-8", 1);//设置时区(中国东八区)
 
	TZ = getenv("TZ");
	printf("====================================> TZ = %s\n", TZ == NULL?"env TZ not seted" : TZ );
 
	now = time(NULL);
	printf("time_t = %ld, ctime: = %s", now, ctime(&now) ); 
	gettimeofday(&tval, &tzone);
	printf("gettimeofday tval.tv_sec = %ld, tval.tv_usec = %ld; tzone.tz_minuteswest = %d, tzone.tz_dsttime = %d.\n", \
			tval.tv_sec, tval.tv_usec, tzone.tz_minuteswest, tzone.tz_dsttime);
	
	tm_ = gmtime(&now);            
	printf("gmtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec);   
 
	tm_ = localtime(&now);        
	printf("localtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec); 
	printf("\n");
 
 
	setenv("TZ", "NZST-12:00:00", 1);//设置时区(新西兰标准时间) 
	TZ = getenv("TZ");
	printf("====================================> TZ = %s\n", TZ == NULL?"env TZ not seted" : TZ );
 
	now = time(NULL);
	printf("time_t = %ld, ctime: = %s", now, ctime(&now) ); 
	gettimeofday(&tval, &tzone);
	printf("gettimeofday tval.tv_sec = %ld, tval.tv_usec = %ld; tzone.tz_minuteswest = %d, tzone.tz_dsttime = %d.\n", \
			tval.tv_sec, tval.tv_usec, tzone.tz_minuteswest, tzone.tz_dsttime);
	
	tm_ = gmtime(&now);            
	printf("gmtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec);   
 
	tm_ = localtime(&now);          
	printf("localtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec); 
	printf("\n");
    

    printf("====================================> SET TZ =GMT-8\n" );
    setenv("TZ", "GMT-8", 1);
	TZ = getenv("TZ");
	printf("====================================> TZ = %s\n", TZ == NULL?"env TZ not seted" : TZ );
 
	now = time(NULL);
	printf("time_t = %ld, ctime: = %s", now, ctime(&now) ); 
	gettimeofday(&tval, &tzone);
	printf("gettimeofday tval.tv_sec = %ld, tval.tv_usec = %ld; tzone.tz_minuteswest = %d, tzone.tz_dsttime = %d.\n", \
			tval.tv_sec, tval.tv_usec, tzone.tz_minuteswest, tzone.tz_dsttime);
	
	tm_ = gmtime(&now);            
	printf("gmtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec);   
 
	tm_ = localtime(&now);          
	printf("localtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec); 
	printf("\n");


 
	/**
	 * 
	 * NZDT-13:00:00,M10.1.0,M3.3.0
	 * 
	 * NZST，表示新西兰标准时间；
	 * 
	 * -12:00:00表示比UTC早12小时；
	 * 
	 * NZDT，表示新西兰夏时制时间；
	 * 
	 * -13:00:00，表示夏时制时间比UTC早13小时（即比标准时间快1小时）；
	 * 
	 * M10.1.0,M3.3.0表示夏令时从10月第一个星期日02:00:00开始到3月第三个星期日02:00:00结束。
	 * ==>m：指一年中的第几月（1 <= m <= 12）。
	 * ==>w：指一个月中的第几周（1 <= w <= 5），1表示第一周，5表示最后一周。
	 * ==>d：指一周中的第几天（0 <= d <= 6），0表示星期天。
	 * ==>time：该字段表示本地时间对夏时制生效或结束的时间，如果省略默认为02:00:00。
	 * 
	*/
	setenv("TZ", "NZST-12:00:00NZDT-13:00:00,M10.1.0,M3.3.0", 1);//设置时区(新西兰夏令时时间) 
	TZ = getenv("TZ");
	printf("====================================> TZ = %s\n", TZ == NULL?"env TZ not seted" : TZ );
 
	//while(1)
	{
		now = time(NULL);
		printf("time_t = %ld, ctime: = %s", now, ctime(&now) ); 
		gettimeofday(&tval, &tzone);
		printf("gettimeofday tval.tv_sec = %ld, tval.tv_usec = %ld; tzone.tz_minuteswest = %d, tzone.tz_dsttime = %d.\n", \
				tval.tv_sec, tval.tv_usec, tzone.tz_minuteswest, tzone.tz_dsttime);
		
		tm_ = gmtime(&now);            
		printf("gmtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec);   
 
		tm_ = localtime(&now);         
		printf("localtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec); 
		printf("\n");
		sleep(5);
	}
	return 0;
    /*
    ————————————————
版权声明：本文为CSDN博主「modi000」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/modi000/article/details/125048471
    */
}


int set_tz_env_test3(void)
{
    time_t now = 0; char * TZ = NULL;  struct tm *tm_ = NULL; struct timeval tval = {0,0}; struct timezone tzone = {0,0};

	// setenv("TZ", "CST-8", 1);//设置时区(中国东八区)
 	set_time_zone(-8);//设置伦敦时区（0时区），相当于UTC时间
	TZ = getenv("TZ");
	printf("====================================> TZ = %s\n", TZ == NULL?"env TZ not seted" : TZ );
 
	now = time(NULL);
	printf("time_t = %ld, ctime: = %s", now, ctime(&now) ); 
	gettimeofday(&tval, &tzone);
	printf("gettimeofday tval.tv_sec = %ld, tval.tv_usec = %ld; tzone.tz_minuteswest = %d, tzone.tz_dsttime = %d.\n", \
			tval.tv_sec, tval.tv_usec, tzone.tz_minuteswest, tzone.tz_dsttime);
	
	tm_ = gmtime(&now);            
	printf("gmtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec);   
 
	tm_ = localtime(&now);        
	printf("localtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec); 
	printf("\n");
    printf("====================================>set_time_zone TEST\n" );
    set_time_zone(0);//设置伦敦时区（0时区），相当于UTC时间
    TZ = getenv("TZ");
	printf("====================================> TZ = %s\n", TZ == NULL?"env TZ not seted" : TZ );
 
	now = time(NULL);
	printf("time_t = %ld, ctime: = %s", now, ctime(&now) ); 
	gettimeofday(&tval, &tzone);
	printf("gettimeofday tval.tv_sec = %ld, tval.tv_usec = %ld; tzone.tz_minuteswest = %d, tzone.tz_dsttime = %d.\n", \
			tval.tv_sec, tval.tv_usec, tzone.tz_minuteswest, tzone.tz_dsttime);
	
	tm_ = gmtime(&now);            
	printf("gmtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec);   
 
	tm_ = localtime(&now);          
	printf("localtime: dstflag = %d, %d-%d-%d %d:%d:%d\n",tm_->tm_isdst, tm_->tm_year + 1900, tm_->tm_mon+1, tm_->tm_mday, tm_->tm_hour, tm_->tm_min, tm_->tm_sec); 
    return 0;
}
//设置本地时间
int set_local_time_test(void)
{
	int ret =0;
	time_t cur_sec;
	struct tm *current_time_p;
	struct tm tm_time;

	cur_sec =time(NULL);
	printf("current:date time:\n");
	system("date");
	sleep(1);
	current_time_p= localtime_r(&cur_sec, &tm_time);
	// printf("current_time_p=%x,&tm_time=%x\n",(long)current_time_p, (long)&tm_time);
	tm_time.tm_hour ++;
	tm_time.tm_hour %=60;

	// printf("change hour...\n");
	cur_sec = mktime(&tm_time);
	/* set kernel time */
	stime(&cur_sec);
	printf("after change hour:\n");
	system("date");
	sleep(1);
	/* set hw-rtc time */
	//ret = ioctl(fd, RTC_SET_TIME, &tm_time);
	return 0;
}
static const char ac_option_hint[  ][ LEN_HINT ] = {
    "	测试设置本地时间" ,
    "	测试修改TZ环境变量" ,
    "	帮助" ,
    ""
};

enum{
    ARG_CHANGE_LOCAL_TIME_TEST=0xffff,
    ARG_CHANGE_TZ_ENV_TEST,
  
};
static int option_flag=0xffff;
static const struct option option_long[ ] = {
    { "local_time"        , no_argument       , (void*)&option_flag , ARG_CHANGE_LOCAL_TIME_TEST} , //"       打印帮助信息" ,
    { "tz_env"        , no_argument       , (void*)&option_flag , ARG_CHANGE_TZ_ENV_TEST} , //"       打印帮助信息" ,
    { "help"        , no_argument , NULL , 'h'} , //"[PATH] 文件保存路径" ,
    {0, 0, 0, 0}
};
/*
 * help_hint: use the -h --help option.Print option of help information
 * return: 0
 */
static int help_hint( char *pc_prog_name )
{
    int i;

    printf( "usage: \r\n  %s  [options] <value>\n\n", pc_prog_name);
    printf( "options: \r\n");
    for(i = 0; i < sizeof(option_long) / sizeof(struct option) - 1; i ++)
    {
		if((option_long[ i ].val )<ARG_CHANGE_LOCAL_TIME_TEST){
			printf("  -%c,--%-16s\t%s \n" , option_long[ i ].val , option_long[ i ].name , ac_option_hint[ i ]);
		}else{
			printf("   --%-16s\t%s \n" , option_long[ i ].name , ac_option_hint[ i ]);
		}
    }
    printf( "\nexample: \r\n"
        "  %s --local_time \n", pc_prog_name);
    printf("\n\n");
    return 0;
}
static char *get_option_short( struct option *p_option, int i_num_option, char *pc_option_short,
                        int i_len_option )
{
    int i;
    int i_offset = 0;
    char c_option;

    for( i = 0 ; i < i_num_option ; i ++ )
    {
        c_option = p_option[ i ].val;
        if(c_option >0){
            switch( p_option[ i ].has_arg )
            {
            case no_argument:
                i_offset += snprintf( pc_option_short + i_offset , i_len_option - i_offset , "%c" , c_option );
                break;
            case required_argument:
                i_offset += snprintf( pc_option_short + i_offset , i_len_option - i_offset , "%c:" , c_option );
                break;
            case optional_argument:
                i_offset += snprintf( pc_option_short + i_offset , i_len_option - i_offset , "%c::" , c_option );
                break;
            }
        }
        
    }
    return pc_option_short;
}
static int parse_option( int argc, char **argv )
{
    int i_option;
    char ac_option_short[ LEN_OPTION_SHORT ];
    int i_array_num = sizeof( option_long ) / sizeof( struct option ) ;
    char c_flag = 1;
    int option_index=0;
    if (argc ==1)
    {
        help_hint( argv[ 0 ] );
        c_flag = 0;
        goto parse_option_end;
    }

#ifdef AK_RTOS
    optind = 0;
#endif
    get_option_short( (struct option*)option_long, i_array_num , ac_option_short , LEN_OPTION_SHORT );
    // printf("ac_option_short=%s\n",ac_option_short);
    while(1)
    {
        i_option = getopt_long(argc , argv , ac_option_short , option_long , &option_index) ;
        if(i_option==-1){
            break;
        }
        DEBUG_LOG("option_index=%d\n",option_index);
        if(i_option==0){
            
            if(i_option < ARRAY_SIZE(option_long)){
                DEBUG_LOG("option_index[%u]=%s\n",option_index, option_long[option_index].name);
                switch (option_flag)
                {
      
                case ARG_CHANGE_LOCAL_TIME_TEST:
                    test_mode =option_flag;
                    break;
				
                case ARG_CHANGE_TZ_ENV_TEST:
                    test_mode =option_flag;
                    break;
                
                default:
                    break;
                }
            }
        }else{
            switch(i_option)
            {
            case 'h' :                                                          //help
                help_hint( argv[ 0 ] );
                c_flag = 0;
                goto parse_option_end;
           
        
            default :                                                          //path
                help_hint( argv[ 0 ] );
                c_flag = 0;
                goto parse_option_end;
                break;
            }
            
        }
        
    }
parse_option_end:
    return c_flag;
}
int main(int argc, char *argv[])
{
	teatall();
	return 0;
	if(0==parse_option(argc, argv)){
        return -1;
    }
	switch (test_mode)
	{
	case ARG_CHANGE_LOCAL_TIME_TEST:
		set_local_time_test();
		break;
	
	case ARG_CHANGE_TZ_ENV_TEST:
		set_tz_env_test3();
		break;
	
	default:
		break;
	}	
    return 0;
}


int test_get_time_apis(void);
int test_set_time_apis(void);

#define TEST_FUNC(f) {printf( "test " #f " \n");f();}
static int teatall(void)
{
	// TEST_FUNC(test1);
	// TEST_FUNC(test2);
	// TEST_FUNC(set_tz_env_test3);
	test_get_time_apis();
	TEST_FUNC(test_get_time_apis);
	TEST_FUNC(test_set_time_apis);

    return 0;
}


int test_get_time_apis(void)
{
	int r=-1;
	char buf[100];
	char* buf_p=NULL;

	time_t time_t_var;
	struct tm struct_tm_var;
	struct tm* struct_tm_p=NULL;
	struct timeval tv;
	struct timezone tz;

	//time() returns the time as the number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
	time_t_var =  time(NULL);
	printf("time() --> %ld\n",time_t_var);

	/*
	The gmtime() function converts the calendar time timep to broken-down time representation,  expressed  in  Coordinated
       Universal  Time  (UTC).   It may return NULL when the year does not fit into an integer.  The return value points to a
       statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions.  The
       gmtime_r() function does the same, but stores the data in a user-supplied struct.
	*/
	struct_tm_p = gmtime(&time_t_var);
	printf("gmtime():\n");
	print_tm(struct_tm_p);
	printf("\n\n");


	struct_tm_p = gmtime_r(&time_t_var,&struct_tm_var);
	printf("gmtime_r(): struct_tm_p==&struct_tm_var :%d\n",struct_tm_p == ( &struct_tm_var) );
	print_tm(struct_tm_p);
	printf("\n\n");

	/*
	the  localtime()  function  converts the calendar time timep to broken-down time representation, expressed relative to
       the user's specified timezone.  The function acts as if it called tzset(3) and sets the external variables tzname with
       information  about  the  current  timezone,  timezone with the difference between Coordinated Universal Time (UTC) and
       local standard time in seconds, and daylight to a nonzero value if daylight savings time rules apply during some  part
       of  the year.  The return value points to a statically allocated struct which might be overwritten by subsequent calls
       to any of the date and time functions.  The localtime_r() function does the same, but stores the data in  a  user-sup‐
       plied struct.  It need not set tzname, timezone, and daylight.
	*/
	struct_tm_p = localtime(&time_t_var);
	printf("localtime():\n");
	print_tm(struct_tm_p);
	printf("\n\n");

	struct_tm_p = localtime_r(&time_t_var,&struct_tm_var);
	printf("localtime_r(): struct_tm_p==&struct_tm_var :%d\n",struct_tm_p == ( &struct_tm_var) );
	print_tm(struct_tm_p);
	printf("\n\n");

	/* The  asctime()  function  converts the broken-down time value tm into a null-terminated string with the same format as
       ctime().  The return value points to a statically allocated string which might be overwritten by subsequent  calls  to
       any  of the date and time functions.  The asctime_r() function does the same, but stores the string in a user-supplied
       buffer which should have room for at least 26 bytes.
	*/
	printf("asctime():%s\n",asctime(struct_tm_p));

	memset(buf,0,sizeof(buf));
	buf_p = asctime_r(struct_tm_p,buf);
	printf("asctime_r():%d %s\n",buf==buf_p,buf);
	/*
	The  functions gettimeofday() and settimeofday() can get and set the time as well as a timezone.  The tv argument is a
       struct timeval (as specified in <sys/time.h>):
	*/
	r= gettimeofday(&tv ,&tz);
	if(r <0) {
		printf("gettimeofday failed,:%s\n",strerror(errno));
	}
	printf("gettimeofday():\n");
	print_tv(&tv);
	print_tz(&tz);
	printf("\n\n");

	return 0;
}

int test_set_time_apis(void)
{
	int r=-1;
	struct timeval tv;
	time_t secs=0;
	printf("before settimeofday\n");
	system("date -R");
	tv.tv_sec = time(NULL)+60*60;
	r= settimeofday(&tv ,NULL);
	if(r <0) {
		printf("gettimeofday failed,:%s\n",strerror(errno));
	}
	printf("after settimeofday\n");
	system("date -R");

	printf("before stime\n");
	system("date -R");
	secs=3600+time(NULL);
	r = stime(&secs);
	if(r!=0){
		printf("stime failed,%s\n",strerror(errno));
	}
	printf("after stime\n");
	system("date -R");

	return 0;
}
